// import { routerRedux } from 'dva/router';
import { message } from 'antd';
import echarts from 'echarts';
import { getDrawData } from '@/services/evaluate';
import initChart from '@/utils/initChart';
import { generateProfitChartOption } from '@/utils/echarts-figures.js';
// import { debug } from 'util';
import { filterDrawData } from '../filterData';

// 检查命名空间是否正确
const isValidNS = ns => ['self', 'compare'].indexOf(ns) >= 0;
export default {
  namespace: 'drawPage',

  state: {
    self: {
      // 本公司作图
      count: 0,
      options: {},
    },
    compare: {
      // 对比公司作图
      count: 0,
      companies: [],
      options: {},
    },
  },

  effects: {
    *drawChart({ payload }, { call, put, select }) {
      try {
        const namespace = payload.namespace || 'self';
        if (!isValidNS(namespace)) {
          return;
        }
        const count = yield select(state => state.drawPage[namespace].count);
        console.log('目前的状态：', count);
        const res = yield call(getDrawData, payload);
        if (+res.code !== 0) {
          message.error(res.msg);
          return;
        }
        const chartData = filterDrawData(res.data, payload);
        if (!chartData || !chartData.data) {
          message.error('暂无数据');
          return;
        }
        let name = '';
        // type 数据类型 item 展示的科目 timeParam 时间区间
        if (payload.type.length !== 0) {
          name = payload.item + '-' + payload.type + '-' + payload.timeParam;
        } else {
          name = payload.item + '-' + payload.timeParam;
        }
        if (count == 0) {
          // 初次绘制
          let options = generateProfitChartOption(
            '自由作图区',
            name,
            chartData.data.dates,
            chartData.data.values,
            payload.type
          );
          yield put({
            type: 'setOptions',
            payload: {
              count: 1,
              options,
              namespace,
            },
          });
          initChart(options, payload.chartId);
        } else {
          // 合并作图结果
          yield put({
            type: 'combineData',
            payload: {
              namespace,
              line: {
                name,
                type: 'line',
                smooth: true,
                data: chartData.data.values,
                xAxis: {
                  data: chartData.data.dates,
                },
              },
            },
          });
          let options = yield select(state => {
            console.log(state);
            return state.drawPage[namespace].options;
          });
          initChart(options, payload.chartId);
        }
      } catch (e) {
        console.log(e);
        message.error('获取数据失败，请稍后再试');
        // yield put({type: "USER_FETCH_FAILED", message: e.message});
      }
    },
    *resetData({ payload }, { call, put, select }) {
      const namespace = payload.namespace || 'self';
      if (!isValidNS(namespace)) {
        return;
      }
      let drawChart = echarts.getInstanceByDom(document.getElementById(payload.chartId));
      drawChart.clear();
      let options = drawChart.getOption();
      yield put({
        type: 'resetChartData',
        payload: {
          count: 0,
          options,
          namespace,
        },
      });
    },
  },
  reducers: {
    // 合并作图选项
    combineData(state, { payload }) {
      const { namespace } = payload;
      state[namespace].options.series.push(payload.line);
      const newState = {
        ...state,
        [namespace]: {
          options: {
            ...state[namespace].options,
          },
        },
      };
      return newState;
    },
    // 设置作图选项
    setOptions(state, { payload }) {
      const { namespace, options, count } = payload;
      const newState = {
        ...state,
        [namespace]: {
          options,
          count,
        },
      };
      return newState;
    },
    resetChartData(state, { payload }) {
      const { options, count, namespace } = payload;
      const newState = {
        ...state,
        namespace: {
          options,
          count,
        },
      };
      return newState;
    },
  },
};
